<?php

require_once('../../auth/auth.php');

//get values from POST
	$apt_number = $_POST['apt_number'];
	$street_address = $_POST['street_address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zipcode = $_POST['zipcode'];
	$num_adults = $_POST['num_adults'];
	$num_children = $_POST['num_children'];
	$other_agency = $_POST['other_agency'];
	$thanksgiving_basket = $_POST['thanksgiving_basket'];
	$food_only = $_POST['food_only'];
	$other = $_POST['other'];
	
	$first_name1 = $_POST['first_name1'];
	$middle_initial1 = $_POST['middle_initial1'];
	$last_name1 = $_POST['last_name1'];
	$gender1 = $_POST['gender1'];
	$date_of_birth1 = $_POST['date_of_birth1'];
	$phone_number1 = $_POST['phone_number1'];
	$last_four_SSN1 = $_POST['last_four_SSN1'];
	$relationship1 = $_POST['relationship1'];
	
	$first_name2 = $_POST['first_name2'];
	$middle_initial2 = $_POST['middle_initial2'];
	$last_name2 = $_POST['last_name2'];
	$gender2 = $_POST['gender2'];
	$date_of_birth2 = $_POST['date_of_birth2'];
	$phone_number2 = $_POST['phone_number2'];
	$last_four_SSN2 = $_POST['last_four_SSN2'];
	$relationship2 = $_POST['relationship2'];
	
	$personalUpdate = !isset($_POST['id']);  	
 	
	if ($personalUpdate){
		 	$user_id = $_SESSION['SESS_USER_ID'];	
	} else {
			$user_id=$_POST['id'];	
	}
	
	$created = date('Y-m-d H:i:s');
	
include 'db_Connect.php';


$family= "INSERT INTO Families
			(apt_number, street_address, city, state, zipcode, num_adults, num_children, other_agency, thanksgiving_basket, food_only, other, created_by_user_id, application_date) 
		VALUES 
		('$apt_number', '$street_address', '$city', '$state', '$zipcode', '$num_adults', '$num_children', '$other_agency', '$thanksgiving_basket', '$food_only', '$other', '$user_id', '$created')";

$result = mysql_query($family);

if(!$result){
	echo("<br> Family create failed");
}

// GET FAMILY ID
//$family_id_result = mysql_query("SELECT MAX(family_id) FROM Families;");
//$family_id_row = mysql_fetch_array($family_id_result);
//$family_id = $family_id_row['family_id'];
$fetchIdSql = "SELECT * 
				FROM Families
				WHERE created_by_user_id = '$user_id'
				ORDER BY application_date DESC LIMIT 1";
$fetchIdResult = mysql_query($fetchIdSql);
$fetchId = mysql_fetch_array($fetchIdResult);
$family_id = $fetchId['family_id'];

//echo($family_id);

$member1= "INSERT INTO FamilyMembers 
			(family_id, first_name, middle_initial, last_name, gender, date_of_birth, contact_1, contact_2, last_four_SSN, relationship)
		VALUES 
		('$family_id', '$first_name1', '$middle_initial1', '$last_name1', '$gender1', '$date_of_birth1', 1, 0, '$last_four_SSN1', '$relationship1')";
			
$result1 = mysql_query($member1);


if(!$result1){
	echo("<br> Member1 create failed");
}


$member2= "INSERT INTO FamilyMembers (family_id, first_name, middle_initial, last_name, gender, date_of_birth, contact_1, contact_2, last_four_SSN, relationship)
		  VALUES
			('$family_id', '$first_name2', '$middle_initial2', '$last_name2', '$gender2', '$date_of_birth2', '0', '1', '$last_four_SSN2', '$relationship2')";

$result2 = mysql_query($member2);
if($result2){
	echo("<br> Family created succeeded!");
	header("Location:../member.php?family_id=$family_id");
	}
else{
	echo("<br> Member2 create failed!");
}

include 'db_Close.php';
?>
























